#from kivymd.app import MDApp
from kivymd.uix.snackbar import Snackbar
from kivymd.uix.button import MDFlatButton
#from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.bottomnavigation import MDBottomNavigationItem
#from kivymd.uix.screen import Screen
from kivy.core.window import Window
#from kivy.properties import StringProperty, NumericProperty
#from kivy.clock import Clock
#from kivy.lang import Builder
from wifi_kits import *
#import time


def errorSnackbar(error_text):
    snackbar = Snackbar(
        text=error_text,
        snackbar_x="10dp",
        snackbar_y="10dp",
    )
    snackbar.size_hint_x = (
        Window.width - (snackbar.snackbar_x * 2)
    ) / Window.width
    snackbar.buttons = [
        MDFlatButton(
            text="[color=#ffffff]RETRY[/color]",
            on_release=snackbar.dismiss,
        ),
    ]
    snackbar.open()


class Screen_wifi_interface(MDBottomNavigationItem):
    def on_enter(self, *args):
        print("Wifi initiated!")
        self.linkStatus()

    def plusTargetTemp(self, temp):
        temp += self.target_temp
        if temp > 80:
            errorSnackbar("Error: You've reached the maximal temperature")
        elif temp < 0:
            errorSnackbar("Error: You've reached the minimal temperature!")
        else:
            self.target_temp = temp

    def setTemperature(self):
        text = set_temperature(self.target_temp)
        if text == "Error":
            errorSnackbar("Error: Failed to set target temperature!")
        else:
            self.set_temp = str(format(self.target_temp, ".2f"))

    def getTemperature(self):
        text = get_temperature()
        if text == "Error":
            errorSnackbar("Error: Failed to get target temperature!")
        else:
            self.current_temp = text

    def getSetTemperature(self):
        text = get_set_temperature()
        if text == "Error":
            errorSnackbar("Error: Failed to get preset target temperature!")
        else:
            self.set_temp = text

    def reversePin(self, pin):
        text = reverse_pin(pin)
        if text == "Error":
            errorSnackbar("Error: Failed to set this function!")
            self.colors[pin] = "gray"
        else:
            self.status[pin] = text
            if text == "ON":
                self.colors[pin] = "lightgreen"
            else:
                self.colors[pin] = "red"

    def linkStatus(self):
        print('linkStatus() called!')
        if link_status():
            self.getTemperature()
            self.getSetTemperature()
        else:
            errorSnackbar("Error: Failed to connect to the MRM machine!")
            
        print('linkStatus() finished!')



'''class iGEMApp(MDApp):
    def build(self):
        self.root = Builder.load_file("wifi.kv")

    def on_start(self):
        self.root.linkStatus()


if __name__ == "__main__":
    iGEMApp().run()'''
